package com.csgc.camera.biz.other.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.csgc.camera.bean.MaskItemInfo
import com.csgc.camera.biz.vip.VipCenterActivity

import com.csgc.camera.events.showMaskEditDialogEvent
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.storage.UserPreference
import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.databinding.ItemTypeMaskBinding

import com.drake.channel.sendEvent

class MaskTypeAdapter(val context:Context,val list:ArrayList<MaskItemInfo> = ArrayList()):Adapter<MaskTypeAdapter.MaskTypeHolder>() {

    var listener:OnMaskClickListener?=null
    var currentMaskId = -1
    var currentMaskType = -1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MaskTypeHolder {
        return MaskTypeHolder(ItemTypeMaskBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: MaskTypeHolder, position: Int) {
        val data = list[position]
        holder.binding.apply {
            val icon =  context.resources.getIdentifier(data.drawable, "drawable", context.packageName)
            ivMask.setImageResource(icon)

            if(data.type == currentMaskType && data.id == currentMaskId){
                layoutEditMask.visibility = View.VISIBLE
            }else{
                layoutEditMask.visibility = View.GONE
            }
//            if (position == 0 || UserPreference.instance.isVip()){
//                ivVipFlag.visibility = View.INVISIBLE
//            }else{
//                ivVipFlag.visibility = View.VISIBLE
//            }
            stvEditMask.clickWithTrigger {
                sendEvent(showMaskEditDialogEvent(true))
            }
            root.clickWithTrigger {
//                if (position > 0 && !UserPreference.instance.isVip()){
//                    VipCenterActivity.launch(context)
//                    return@clickWithTrigger
//                }
                MaskPreference.instance.setMaskType(data.type)
                MaskPreference.instance.setMaskId(data.id)
                listener?.onMaskClick(data.type,data.id)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class MaskTypeHolder(val binding:ItemTypeMaskBinding):ViewHolder(binding.root)
    interface OnMaskClickListener{
        fun onMaskClick(type:Int,id:Int)
    }
}